package j_util.sorters;

import java.util.Comparator;
import java.util.List;

public class SortingUtils {
    public static <T> boolean less(T u, T w, Comparator<T> c) {
        return c.compare(u, w) < 0;
    }

    public static <T> void exchange(List<T> list, int i, int j) {
        T temp = list.get(i);
        list.set(i, list.get(j));
        list.set(j, temp);
    }

    public static <T> boolean compareAndExchange(List<T> list, int left, int right, Comparator<T> comparator) {
        if (less(list.get(right), list.get(left), comparator)) {
            exchange(list, left, right);
            return true;
        }
        return false;
    }

    public static <T> boolean equal(T u, T w, Comparator<T> c) {
        return (!less(u, w, c) && !less(w, u, c));
    }

    public static int min(int a, int b) {
        if (a < b) {
            return a;
        }
        return b;
    }
}
